﻿using System;
using System.IO;

namespace PolePosition.v3.WcfService.App_Code
{
    public class KmlCacheBuilder
    {
        public string Key { get; private set; }
        public string Type { get; private set; }
        public string SPName { get; private set; }
        public string RootPath { get; private set; }

        public string KmlMapFullPath
        {
            get
            {
                return RootPath + KmlMapFile;
            }
        }

        public string KmlMapFile
        {
            get { return string.Format("{0}.{1}.kml", Type, Key); }
        }

        //Object to lock file access while writing kml
        protected  static Object FileLockObj = new Object();

        public KmlCacheBuilder(string type, string key, string root)
        {
            Key = key;
            Type = type;
            RootPath = root;
        }

        public void ClearKmlFile()
        {
            if (File.Exists(KmlMapFullPath))
            {
                lock(FileLockObj) {
                    File.Delete(KmlMapFullPath);
                }
            }
        }

        public string GetContent()
        {
            try
            {
                if (!File.Exists(KmlMapFullPath))
                    CreateKmlFile();

                return File.ReadAllText(KmlMapFullPath);
            }
            catch
            {
                return string.Empty;
            }
        }

        public void CreateKmlFile()
        {
            var commandText = GetKmlMapCacheStrategy();

            if (commandText != string.Empty)
            {
                var _SqlCommand = new System.Data.SqlClient.SqlCommand();

                _SqlCommand.CommandText = GetKmlMapCacheStrategy();
                _SqlCommand.Parameters.AddWithValue("@id", Convert.ToInt32(Key));

                var _kml = ProjectCommon2.Helpers.SqlHelper.ExecuteScalar<string>(_SqlCommand);
                var _path = KmlMapFullPath;

                lock (FileLockObj)
                {
                    File.WriteAllText(_path, _kml);
                }
            }
        }

        public string GetKmlMapCacheStrategy()
        {
            string spName = string.Empty;

            switch (Type)
            {
                case "XAssets":
                    spName = "up_KML_AssetXmissionStructures";
                    break;
                case "DAssets":
                    spName = "up_KML_AssetDistributionPoles";
                    break;
                case "DLines":
                    spName = "up_KML_AssetDistributionPoleLines";
                    break;
                case "GpsVisitDistributionProject":
                    spName = "up_KML_GpsVisitDistributionPole";
                    break;
                case "GpsVisitXMissionProject":
                    spName = "up_KML_GpsVisitXmissionStructures";
                    break;
                case "InspVisitDistributionProject":
                    spName = "up_KML_InspVisitDistributionPoles";
                    break;
                case "InspVisitXmissionVisitProject":
                    spName = "up_KML_InspVisitXmissionStructures";
                    break;
                case "Maps":
                case "Sections":
                    break;
            }

            return spName;
        }
    }
}
